const fs = require('fs');
const { join } = require('path');
const moduleDir = join(__dirname, 'src/module');

const rename = (sourceName, targetName) => {
  const sourceDir = join(moduleDir, sourceName);
  const targetDir = join(moduleDir, targetName);

  const sourceDirFileList = fs.readdirSync(sourceDir, 'utf8');

  fs.mkdirSync(targetDir);

  const sourceVarName = sourceName.replace(/-[a-z]/g, (val)=>{
    return val[1].toUpperCase();
  });
  const sourceClassName = sourceVarName.replace(sourceVarName[0],sourceVarName[0].toUpperCase());
  const targetVarName = targetName.replace(/-[a-z]/g, (val)=>{
    return val[1].toUpperCase();
  });
  const targetClassName = targetVarName.replace(targetVarName[0],targetVarName[0].toUpperCase());

  sourceDirFileList.forEach(sourceDirFileItemName=>{
    const sourceDirFileItemContent = fs.readFileSync(join(sourceDir, sourceDirFileItemName), 'utf8');

    console.log('sourceName',sourceName, targetName);
    console.log('sourceVarName',sourceVarName, targetVarName);
    console.log('sourceClassName',sourceClassName, targetClassName);

    const targetDirFileItemContent = sourceDirFileItemContent.replace(
      // new RegExp(`(${sourceName})|(${sourceName}\/${sourceName})|(v1\/${sourceName})|(${sourceVarName})|(${sourceClassName})`,'g'),
      new RegExp(`(${sourceName})|(${sourceVarName})|(${sourceClassName})`,'g'),
      (old)=>{
      console.log('old');
      console.log(old);
      if(old === sourceName){
        return targetName;
      }
      /*if(old === `${sourceName}/${sourceName}`){
        return `${targetName}/${targetName}`;
      }
      if(old === `v1/${sourceName}`){
        return `v1/${targetName}`;
      }*/
      if(old === sourceVarName){
        return targetVarName;
      }
      return targetClassName;
    })

    fs.writeFileSync(
      join(targetDir, sourceDirFileItemName.replace(sourceName, targetName)),
      targetDirFileItemContent,
      'utf-8'
    );
  });
};

rename('brand', 'track');
